<template>
  <div class="page-container">
    <button class="back-button" @click="goToHome">
      <span class="icon-arrow-left"></span>
      返回首页
    </button>
    
    <div class="page-header">
      <h1>岗位设置</h1>
      <p>配置公司所有岗位信息，包括岗位名称、职责描述和所属部门</p>
    </div>
    
    <div class="action-bar">
      <button class="add-btn" @click="showAddDialog = true">
        <span class="icon-plus"></span> 新增岗位
      </button>
    </div>
    
    <div class="positions-container">
      <div class="department-section" v-for="dept in departments" :key="dept.id">
        <div class="department-header">
          <h3>{{ dept.name }}</h3>
          <span class="count-badge">{{ dept.positions.length }} 个岗位</span>
        </div>
        
        <div class="positions-grid">
          <div class="position-card" v-for="pos in dept.positions" :key="pos.id">
            <div class="card-header">
              <h4>{{ pos.name }}</h4>
              <div class="card-actions">
                <button class="edit-icon" @click="editPosition(dept.id, pos)">✎</button>
                <button class="delete-icon" @click="deletePosition(dept.id, pos.id)">✕</button>
              </div>
            </div>
            <div class="card-body">
              <p class="position-level"><strong>级别：</strong>{{ pos.level }}</p>
              <p class="position-desc"><strong>职责：</strong>{{ pos.description }}</p>
            </div>
            <div class="card-footer">
              <span class="staff-count">{{ pos.staffCount }} 名员工</span>
            </div>
          </div>
          
          <div class="add-position-card" @click="openAddPositionDialog(dept.id)">
            <div class="add-icon">+</div>
            <p>添加岗位</p>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 岗位弹窗 -->
    <div class="dialog-mask" v-if="showDialog">
      <div class="dialog-container">
        <div class="dialog-header">
          <h3>{{ isEditMode ? '编辑岗位' : '新增岗位' }}</h3>
          <button class="close-btn" @click="closeDialog">×</button>
        </div>
        <div class="dialog-body">
          <form class="position-form">
            <div class="form-group">
              <label>所属部门</label>
              <select v-model="formData.departmentId" :disabled="isEditMode">
                <option value="">请选择部门</option>
                <option v-for="dept in departments" :value="dept.id" :key="dept.id">
                  {{ dept.name }}
                </option>
              </select>
            </div>
            <div class="form-group">
              <label>岗位名称</label>
              <input type="text" v-model="formData.name" required>
            </div>
            <div class="form-group">
              <label>岗位级别</label>
              <select v-model="formData.level">
                <option value="初级">初级</option>
                <option value="中级">中级</option>
                <option value="高级">高级</option>
                <option value="主管">主管</option>
                <option value="经理">经理</option>
                <option value="总监">总监</option>
              </select>
            </div>
            <div class="form-group">
              <label>岗位职责</label>
              <textarea v-model="formData.description" rows="4"></textarea>
            </div>
          </form>
        </div>
        <div class="dialog-footer">
          <button class="cancel-btn" @click="closeDialog">取消</button>
          <button class="confirm-btn" @click="savePosition">确认</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PositionSetting',
  data() {
    return {
      showDialog: false,
      isEditMode: false,
      formData: {
        departmentId: '',
        id: '',
        name: '',
        level: '初级',
        description: '',
        staffCount: 0
      },
      departments: [
        {
          id: 'dept1',
          name: '技术部',
          positions: [
            { id: 'pos1-1', name: '前端开发', level: '中级', description: '负责公司Web前端开发工作', staffCount: 3 },
            { id: 'pos1-2', name: '后端开发', level: '高级', description: '负责公司服务器端开发工作', staffCount: 5 },
            { id: 'pos1-3', name: '技术经理', level: '经理', description: '负责技术团队管理和技术决策', staffCount: 1 }
          ]
        },
        {
          id: 'dept2',
          name: '市场部',
          positions: [
            { id: 'pos2-1', name: '市场专员', level: '初级', description: '负责市场推广活动执行', staffCount: 2 },
            { id: 'pos2-2', name: '市场经理', level: '经理', description: '负责市场策略制定和团队管理', staffCount: 1 }
          ]
        },
        {
          id: 'dept3',
          name: '人事部',
          positions: [
            { id: 'pos3-1', name: '招聘专员', level: '中级', description: '负责公司人才招聘工作', staffCount: 1 },
            { id: 'pos3-2', name: '人事经理', level: '经理', description: '负责人力资源整体管理工作', staffCount: 1 }
          ]
        }
      ]
    };
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    openAddPositionDialog(deptId) {
      this.formData = {
        departmentId: deptId,
        id: '',
        name: '',
        level: '初级',
        description: '',
        staffCount: 0
      };
      this.isEditMode = false;
      this.showDialog = true;
    },
    editPosition(deptId, position) {
      this.formData = { ...position, departmentId: deptId };
      this.isEditMode = true;
      this.showDialog = true;
    },
    deletePosition(deptId, posId) {
      if (confirm('确定要删除该岗位吗？')) {
        const deptIndex = this.departments.findIndex(dept => dept.id === deptId);
        if (deptIndex !== -1) {
          this.departments[deptIndex].positions = this.departments[deptIndex].positions.filter(
            pos => pos.id !== posId
          );
        }
      }
    },
    closeDialog() {
      this.showDialog = false;
    },
    savePosition() {
      if (!this.formData.name || !this.formData.departmentId) return;
      
      const deptIndex = this.departments.findIndex(dept => dept.id === this.formData.departmentId);
      if (deptIndex === -1) return;
      
      if (this.isEditMode) {
        // 编辑模式
        const posIndex = this.departments[deptIndex].positions.findIndex(
          pos => pos.id === this.formData.id
        );
        if (posIndex !== -1) {
          this.departments[deptIndex].positions[posIndex] = { ...this.formData };
        }
      } else {
        // 新增模式
        const newId = `${this.formData.departmentId}-${Date.now().toString().slice(-4)}`;
        this.departments[deptIndex].positions.push({
          ...this.formData,
          id: newId
        });
      }
      
      this.closeDialog();
    }
  }
};
</script>

<style scoped>
.page-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.page-header {
  margin: 20px 0 30px;
}

.page-header h1 {
  color: #2c3e50;
  margin-bottom: 10px;
  font-size: 24px;
}

.page-header p {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0;
}

.action-bar {
  margin-bottom: 30px;
}

.add-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
  background-color: #2ecc71;
  color: white;
  display: flex;
  align-items: center;
  gap: 6px;
}

.add-btn:hover {
  background-color: #27ae60;
}

.department-section {
  margin-bottom: 40px;
}

.department-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 2px solid #f1f1f1;
}

.department-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
}

.count-badge {
  background-color: #e8f4fd;
  color: #3498db;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.positions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.position-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  overflow: hidden;
  transition: transform 0.2s, box-shadow 0.2s;
}

.position-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  background-color: #f8f9fa;
  border-bottom: 1px solid #f1f1f1;
}

.card-header h4 {
  margin: 0;
  color: #2c3e50;
  font-size: 16px;
}

.card-actions {
  display: flex;
  gap: 8px;
}

.edit-icon, .delete-icon {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
}

.edit-icon {
  color: #3498db;
}

.edit-icon:hover {
  background-color: rgba(52, 152, 219, 0.1);
}

.delete-icon {
  color: #e74c3c;
}

.delete-icon:hover {
  background-color: rgba(231, 76, 60, 0.1);
}

.card-body {
  padding: 15px 20px;
}

.position-level {
  margin: 0 0 10px 0;
  color: #7f8c8d;
  font-size: 14px;
}

.position-desc {
  margin: 0;
  color: #2c3e50;
  font-size: 14px;
  line-height: 1.5;
}

.card-footer {
  padding: 10px 20px;
  background-color: #f8f9fa;
  border-top: 1px solid #f1f1f1;
}

.staff-count {
  font-size: 13px;
  color: #7f8c8d;
}

.add-position-card {
  border: 2px dashed #bdc3c7;
  border-radius: 8px;
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  color: #7f8c8d;
}

.add-position-card:hover {
  border-color: #3498db;
  color: #3498db;
  background-color: #f8f9fa;
}

.add-icon {
  font-size: 24px;
  margin-bottom: 10px;
}

/* 弹窗样式 */
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-container {
  background-color: white;
  border-radius: 6px;
  width: 100%;
  max-width: 500px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.dialog-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-header h3 {
  margin: 0;
  color: #2c3e50;
  font-size: 18px;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #7f8c8d;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #e74c3c;
}

.dialog-body {
  padding: 20px;
}

.position-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
  color: #2c3e50;
}

.form-group input, 
.form-group select, 
.form-group textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.form-group textarea {
  resize: vertical;
}

.dialog-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn, .confirm-btn {
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.cancel-btn {
  background-color: #f1f1f1;
  border: 1px solid #ddd;
  color: #7f8c8d;
}

.cancel-btn:hover {
  background-color: #e9e9e9;
}

.confirm-btn {
  background-color: #3498db;
  border: 1px solid #3498db;
  color: white;
}

.confirm-btn:hover {
  background-color: #2980b9;
}

/* 继承并保留原有按钮样式 */
.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}
</style>